/**
 * Private Variable Declaration
 * */
 import Modules.Account.ResellerAccountDetail;
 import Modules.Windows.AdminWindow;
 
 import com.desidea.common.CommonClass;
 import com.desidea.components.grid.SuperGridEvent;
 import com.desidea.components.grid.button.SuperGridButton;
 import com.desidea.components.spinner.BusySpinner;
 import com.desidea.webservice.PartnerService;
 import com.desidea.webservice.ProcessServiceRequestResultEvent;
 
 import flash.events.Event;
 import flash.events.MouseEvent;
 
 import mx.controls.Alert;
 import mx.controls.Button;
 import mx.core.Application;
 import mx.rpc.events.FaultEvent;
 

/**
 * Event Handling - CreationComplete event
 * */
private function windowCreated():void{
	
	BusySpinner.getInstance().play(Application.application.ModuleCvs);

	var newItemBtn:Button = new Button();
	newItemBtn.label = "Create New Reseller Account";
	newItemBtn.addEventListener(MouseEvent.CLICK, function(event:Event):void{
														showDetail(SuperGridButton.MODE_EDIT,"");
													});
	resellerAccountSummaryGrid.toolBar.addChild(newItemBtn);
		

	this.resellerAccountSummaryGrid.addEventListener(SuperGridEvent.CLICKED, gridButtonColumnClicked);

	loadData();
}

private function loadData():void{
	var params:String = "OPERATION=RESELLERACCOUNT_LIST";
//						"&ISACTIVE=1";
//						"&USERID=1";
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForResellerAccountsList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForResellerAccountsList(event:ProcessServiceRequestResultEvent):void{
	var response:XML = XML(event.result);
	lblError.text = "";
	if(response.Result == "E"){
		lblError.text = response.MESSAGE
	}else if(response.RESPONSE.RESULT == 'S'){
		this.resellerAccountSummaryGrid.dataProvider = response;
	}else{
		Alert.show("Unknow Error: Unable to load response");
	}
	setColumnWidths();
	BusySpinner.getInstance().stop();
}

private function partnerServiceFaultHandler(event:FaultEvent):void{
	BusySpinner.getInstance().stop();
	Alert.show(event.fault.toString());
}

private function setColumnWidths():void{
	CommonClass.setColumnWidth(this.resellerAccountSummaryGrid,1, 100, "right");
	CommonClass.setColumnWidth(this.resellerAccountSummaryGrid,2, 200);
	CommonClass.setColumnWidth(this.resellerAccountSummaryGrid,3, 120);
	CommonClass.setColumnWidth(this.resellerAccountSummaryGrid,4, 80,"center");
	CommonClass.setColumnWidth(this.resellerAccountSummaryGrid,5, 90);
	CommonClass.setColumnWidth(this.resellerAccountSummaryGrid,6, 100);
	CommonClass.setColumnWidth(this.resellerAccountSummaryGrid,7, 140);
	CommonClass.setColumnWidth(this.resellerAccountSummaryGrid,8, 80,"center");
}

private function gridButtonColumnClicked(event:SuperGridEvent):void{
	BusySpinner.getInstance().play(Application.application.ModuleCvs);
	handleGridButtonClick(event);
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();
	
	if(mode == SuperGridButton.MODE_EDIT || mode == SuperGridButton.MODE_VIEW || mode == SuperGridButton.MODE_VIEWICON){
		showDetail(mode, event.data.data["key"].toString());
	}else{
		BusySpinner.getInstance().stop();
		Alert.show("Mode : " + mode + " not handled");
	}
	
}

private function showDetail(mode:String, key:String=""):void{
	if(key=="") mode = SuperGridButton.MODE_EDIT; 

	var win:AdminWindow = this.parentDocument as AdminWindow;

	var pad:ResellerAccountDetail = new ResellerAccountDetail();
	pad.mode = win.data.toString().toLowerCase();
	pad.key = key;
	pad.mode = mode;
	win.removeAllChildren();
	win.addChild(pad);
}